// MAIN WINDOW - WITH FUNCTION MAIN.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import net.miginfocom.swing.MigLayout;

public class MainClass extends JFrame implements ActionListener {
	private static final long serialVersionUID = 1548233817403693158L;

	// GUI COMPONENTS
	
	private Log log;
	private ItemViewer accs, proxys;
	private JPanel panel, buttonsPanel;
	private JButton start, stop, loadProxys, loadAccs;
	private MigLayout mainLayout, panelLayout, buttonsPanelLayout;
	private JLabel watkiLabel, timeoutLabel;
	private SpinnerModel watkiSpinModel, timeoutSpinModel;
	private JSpinner watkiSpin, timeoutSpin;
	
	// LOGIC
	
	private SessionInfo sessInfo;
	private ArrayList<Worker> workers;
	
	public MainClass() {
		
		SwingUtilities.invokeLater(new Runnable() { 
			public void run() {
				init();
			}
		});
	
	}
	
	public void init() {
		
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		mainLayout = new MigLayout();
		
		setLayout(mainLayout);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(550, 400);
		
		proxys = new ItemViewer("Numery:");
		add(proxys, "height 65%, width 35%");
		
		accs = new ItemViewer("Konta:");
		add(accs, "height 65%, width 35%");
		
		panel = new JPanel();
		panelLayout = new MigLayout("insets 10px 5% 5px 5%");
		panel.setLayout(panelLayout);
		add(panel, "height 65%, width 30%, wrap");
		
		loadProxys = new JButton("Wczytaj proxy");
		loadProxys.addActionListener(this);
		panel.add(loadProxys, "width 100%, wrap");
		
		loadAccs = new JButton("Wczytaj konta");
		loadAccs.addActionListener(this);
		panel.add(loadAccs, "gapy 5px, width 100%, wrap");
		
		watkiLabel = new JLabel("Ilosc watkow:");
		panel.add(watkiLabel, "gapy 10px, wrap");
		
		watkiSpinModel = new SpinnerNumberModel(5, 1, 128, 1);
		watkiSpin = new JSpinner(watkiSpinModel);
		panel.add(watkiSpin, "width 100%, wrap");
		
		timeoutLabel = new JLabel("Timeout (ms):");
		panel.add(timeoutLabel, "gapy 5px, wrap");
		
		timeoutSpinModel = new SpinnerNumberModel(3000, 1000, 60000, 100);
		timeoutSpin = new JSpinner(timeoutSpinModel);
		panel.add(timeoutSpin, "width 100%, wrap");
		
		buttonsPanel = new JPanel();
		buttonsPanelLayout = new MigLayout("insets 0 0 0 0");
		buttonsPanel.setLayout(buttonsPanelLayout);
		
		panel.add(buttonsPanel, "gapy 10px, height 100%, width 100%");
		
		start = new JButton("START");
		buttonsPanel.add(start, "height 100%, width 50%");
		
		stop = new JButton("STOP");
		buttonsPanel.add(stop, "height 100%, width 50%");
		
		log = new Log("Log:");
		add(log, "height 35%, width 100%, span");
		
		sessInfo = new SessionInfo(accs, proxys);
		
		setVisible(true);
		
		log.addItem("Start programu!");
	};
	
	@Override
	public void actionPerformed(ActionEvent e) {
		Object o = e.getSource();
		
		if (o == loadProxys) {
			sessInfo.loadProxys();
		} else if (o == loadAccs) {
			sessInfo.loadAccs();
		} else if (o == start) {
			
		} else if (o == stop) {
			
		}
		
	}
	
	public static void main(String[] args) {
		new MainClass();
	}

}
